﻿//var cimissPath = "http://10.172.89.55/cimiss-web/api?
//userId=BEXA_YGZX_sxnqzx&pwd=81619505&interfaceId=getSurfEleByTimeRangeAndStaID
//&dataCode=SURF_CHN_MUL_DAY&elements=TEM_Avg,TEM_Max,TEM_Min
//&timeRange=[20190125000000,20190131010000]&staIds=57343&dataFormat=json";
var Cimiss = {
    apiroot : "http://10.172.89.55/cimiss-web/api",
    userId : "BEXA_YGZX_sxnqzx",
    pwd: "81619505",
    dataFormat:"json",
    getCimissApi : function(params){
        var _this = this,
            api = "";

         for(var i in params) {
             api += "&"+ i +"="+ params[i] +"";
         }
        
        return api;
    },
    //接口说明：根据站号（1个或多个）和时间范围（日）查询地面观测要素日数据
    //参数params说明：
        //（1）【elements（必须）】:接口需要返回的字段，1个或多个要素，用“,”隔开。常用的包括
            //年：Year
            //月：Month
            //日：Day
            //平均气温：TEM_Avg
            //最高气温：TEM_Max
            //最低气温：TEM_Min
            //20-08时降水量(毫米):PRE_Time_2008
            //08-20时降水量(毫米):PRE_Time_0820
            //20-20时降水量(毫米):PRE_Time_2020
            //08-08时降水量(毫米):PRE_Time_0808
            //平均相对湿度(百分率):RHU_Avg
            //最小相对湿度(百分率):RHU_Min
        //（2）【timeRange（必须）】：时间段，精确到日，格式如：[20190201000000,2019020010000]标示2019年2月1日-2019年2月5日
        //（3）【staIds（必须）】：查询站点编码，1个或多个，用","隔开。
        //（4）【limitCnt（可选）】返回记录条数，不加就是全部返回
    getSurfEleByTimeRangeAndStaID : function(params,callback){
        var _this = this,
            api  = _this.getCimissApi(params);

        $.ajax({
            url: "http://10.172.13.58:8088/ylsmsapi/getSurfEleByTimeRangeAndStaID?" + api,
            type: "get",
            dataType: "json",
            async: false,
            success: function (result) {
               if(result && callback){
                   callback.callback(null,result);
               }
            }
        });
    },
    //接口说明：根据站号（1个或多个）和时间范围（日）查询地面观测要素日数据
    //参数params说明：
        //（1）【elements（必须）】:接口需要返回的字段，1个或多个要素，用“,”隔开。常用的包括
            //年份：Year
            //月份：Month
            // 日：Day
            //小时：Hour
            //地表以下(土壤、土层)深度:Soil_Depth_BelS
            //土壤体积含水量:SVWC
            //土壤相对湿度:SRHU	
            //土壤重量含水率:SWWC
            //土壤有效水分贮存量:SVMS
        //（2）【timeRange（必须）】：时间段，精确到小时，格式如：[20190205010000,20190205010000]
        //（3）【staIds（必须）】：查询站点编码，1个或多个，用","隔开。
        //（4）【limitCnt（可选）】返回记录条数，不加就是全部返回
    getAgmeEleByTimeRangeAndStaID : function(params,callback){
        var _this = this,
            api  = _this.getCimissApi(params);

        $.ajax({
            url: api + "&interfaceId=getAgmeEleByTimeRangeAndStaID&dataCode=AGME_CHN_SOIL_HOR",
            type: "get",
            dataType: "json",
            async: false,
            success: function (result) {
                if(result && callback){
                    callback.callback(null,result);
                }
            }
        });
    },
    elmts: {
        "TEM_Avg": "SurfEle",
        "TEM_Max": "SurfEle",
        "TEM_Min": "SurfEle",
        "PRE_Time_2008": "SurfEle",
        "PRE_Time_0820": "SurfEle",
        "PRE_Time_2020": "SurfEle",
        "PRE_Time_0808": "SurfEle",
        "RHU_Avg": "SurfEle",
        "RHU_Min": "SurfEle",
        "WIN_S": "SurfEle",
        "WIN_S_Max": "SurfEle",
        "SSH": "SurfEle",

        "SRHU": "AgmeEle",
        "SVWC": "AgmeEle",
        "SWWC": "AgmeEle",
        "SVMS": "AgmeEle",
        "TEM": "TEM-TempHour",
        "TEM_Max": "TEM_Max-TempHour",
        "TEM_Min": "TEM_Min-TempHour",
		"PRE_1h": "Rain-TempHour"
    },
    getCimissData: function( params, callback ){
        var _this = this,
            _requertUrl = _this.getUrl( params );
        $.ajax({
            url: _requertUrl,
            type: "get",
            dataType: "json",
            async: false,
            success: function (result) {
				var tempdata=[];
				var hourdata=[];
				var stationName=[];
				 var myMap=new Map();
				if(result && callback){
					
					var filterData = _this.objToHeavy( result.DS, "Station_Name" );
				   if(_this.elmts[ params.elements ]=="TEM-TempHour"){
					   if(result.DS.length>0){
						   for(var x=0;x<filterData.length;x++){
							   stationName.push(filterData[x].Station_Name)
							   	 tempdata=[];
				                 hourdata=[];
							    for(var j=0;j<result.DS.length;j++){
									if(filterData[x].Station_Name==result.DS[j].Station_Name){
										   tempdata.push(result.DS[j].TEM);
										   var nowdata=result.DS[j].Year+"/"+result.DS[j].Mon+"/"+result.DS[j].Day+" "+result.DS[j].Hour+":00";
										   var dd=_this.gettime(nowdata);
							               hourdata.push(dd);
									
									}	
								}
								myMap.set(filterData[x].Station_Name,tempdata);
						   }
					   }
				   }else if(_this.elmts[ params.elements ]=="TEM_Max-TempHour"){
						   if(result.DS.length>0){
						   for(var x=0;x<filterData.length;x++){
							   stationName.push(filterData[x].Station_Name);
							    tempdata=[];
				                 hourdata=[];
							    for(var j=0;j<result.DS.length;j++){
									if(filterData[x].Station_Name==result.DS[j].Station_Name){
										   tempdata.push(result.DS[j].TEM_Max);
							               var nowdata=result.DS[j].Year+"/"+result.DS[j].Mon+"/"+result.DS[j].Day+" "+result.DS[j].Hour+":00";
										   var dd=_this.gettime(nowdata);
							               hourdata.push(dd);
									}
									
								}
								myMap.set(filterData[x].Station_Name,tempdata);
						   }
					   }
				   }else if(_this.elmts[ params.elements ]=="TEM_Min-TempHour"){
				   if(result.DS.length>0){
						   for(var x=0;x<filterData.length;x++){
							   stationName.push(filterData[x].Station_Name);
							    tempdata=[];
				                 hourdata=[];
							    for(var j=0;j<result.DS.length;j++){
									if(filterData[x].Station_Name==result.DS[j].Station_Name){
										   tempdata.push(result.DS[j].TEM_Min);
							               var nowdata=result.DS[j].Year+"/"+result.DS[j].Mon+"/"+result.DS[j].Day+" "+result.DS[j].Hour+":00";
										   var dd=_this.gettime(nowdata);
							               hourdata.push(dd);
									}
									
								}
								myMap.set(filterData[x].Station_Name,tempdata);
						   }
					   }
				   }else if(_this.elmts[ params.elements ]=="Rain-TempHour"){
					   	   if(result.DS.length>0){
						   for(var x=0;x<filterData.length;x++){
							   stationName.push(filterData[x].Station_Name);
							    tempdata=[];
				                 hourdata=[];
							    for(var j=0;j<result.DS.length;j++){
									if(filterData[x].Station_Name==result.DS[j].Station_Name){
										   tempdata.push(result.DS[j].PRE_1h);
										  //tempdata.push(10*j);
							               var nowdata=result.DS[j].Year+"/"+result.DS[j].Mon+"/"+result.DS[j].Day+" "+result.DS[j].Hour+":00";
										   var dd=_this.gettime(nowdata);
							               hourdata.push(dd);
									}
									
								}
								myMap.set(filterData[x].Station_Name,tempdata);
						   }
					   }
				   }
				  // result.ydata=tempdata;
				    result.xdata=hourdata;
					result.stationName=stationName;
					result.dataMap=myMap;
                   callback.callback(null,result);
               }
            }
        });
    },
    getUrl: function( params ) {
        var _this = this,
            _url = "http://10.172.13.58:8088/ylsmsapi/getSurfEleByTimeRangeAndStaID?";

        switch( _this.elmts[ params.elements ] ) {
            case "SurfEle":
                _url += "interfaceId=getSurfEleByTimeRangeAndStaID&dataCode=SURF_CHN_MUL_DAY";
            break;
            case "AgmeEle":
                    _url += "interfaceId=getAgmeEleByTimeRangeAndStaID&dataCode=AGME_CHN_SOIL_HOR";
            break;
            case "TEM-TempHour":case "TEM_Max-TempHour":case "TEM_Min-TempHour":case "Rain-TempHour":
                _url += "interfaceId=getSurfEleByTimeRangeAndStaID&dataCode=SURF_CHN_MUL_HOR";
                break;
        }

        for( var i in params ) {
            if ( i == "elements" && _this.elmts[ params.elements ] == "AgmeEle" ) {
                _url += "&"+ i +"=Soil_Depth_BelS,"+ params[i] +"";
            }
            else if(i == "elements" && _this.elmts[ params.elements ].indexOf("TempHour")>-1){//小时气温
                _url += "&"+ i +"=City,Station_Name,Hour,Day,Year,Mon,Lon,Lat,"+ params[i] +"";
            }

            else {
                _url += "&"+ i +"="+ params[i] +"";
            }
        }

        return _url;
    },
	objToHeavy: function( Arrs, key ) {
		var newArrs = [];

		for ( var i = 0; i < Arrs.length; i++ ){
		　　var flag = true;

		　　for ( var j = 0; j < newArrs.length; j++ ){
		　　　　if ( Arrs[i][ key ] == newArrs[j][ key ] ) {
		　　　　　　flag = false;
		　　　　}
		　　}

		　　if ( flag ) newArrs.push( Arrs[i] );
		}

		return newArrs;
	},
	gettime:function(nowdata){
		var dd=new Date( new Date(nowdata).getTime() + ( 8* 60 * 60 * 1000 ) );
		return dd.getDate()+"日"+dd.getHours()+"时";
	}
}